﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Weibo2Sfdc.SfWeb;

namespace Weibo2Sfdc
{
    public class WeiboParse
    {
        public WebBrowser web_main;
        public List<WBContentBean> result = new List<WBContentBean>();

        public static TRIXPRO_WB__Keyword__c kwc; //关联的Keyword

    
        private static WBContentBean parse(HtmlElement ele)
        {
            WBContentBean bean = new WBContentBean();

            bean.kwc = kwc; //保存关联的keyword

            //微博ID "feed_list" mid="3486490040623407"
            string stylecls = ele.GetAttribute("className");
            string mid = ele.GetAttribute("mid");
            if (stylecls == "feed_list")
            {
                bean.mid = ele.GetAttribute("mid");
            }

            //微博作者  <a href="http://weibo.com/bbacglkclass2012" title="北京奔驰GLK级SUV" target="_blank" suda-data="key=tblog_search_v4.1&amp;value=weibo_feed_h">
            HtmlElementCollection linkTag = ele.GetElementsByTagName("A");
            foreach (HtmlElement he in linkTag)
            {
                //"id=2773349904"
                string usercard = he.GetAttribute("usercard");
                if (usercard.StartsWith("id="))
                {
                    bean.uid = usercard.Substring("id=".Length, 10); // User ID Length 10
                    bean.name = he.GetAttribute("title");
                    break;
                }
            }

            //微博信息内容 <p node-type="feed_list_content">
            HtmlElementCollection pTag = ele.GetElementsByTagName("p");
            foreach (HtmlElement he in pTag)
            {
                //<p node-type="feed_list_content">
                string content = he.GetAttribute("node-type");
                if (content == "feed_list_content")
                {
                    bean.content = he.InnerHtml;
                    break;
                }
            }
            //创建时间 <a href="http://weibo.com/2773349904/yAi042CaX" title="2012-09-04 10:57" date="1346727463000" class="date" node-type="feed_list_item_date" suda-data="key=tblog_search_v4.1&amp;value=weibo_feed_h">9月4日10:57</a>
            linkTag = ele.GetElementsByTagName("A");
            foreach (HtmlElement he in linkTag)
            {
                //9月4日10:57</a>
                string content = he.GetAttribute("node-type");
                if (content == "feed_list_item_date")
                {
                    //bean.item_date = he.InnerHtml; 
                    bean.item_date = he.GetAttribute("title");//关于微博时间，改为从title属性拿
                    //来自 <a target="_blank" href="http://a.weibo.com/proc/productintro.php" rel="nofollow">企业微博</a>
                    if (he.Parent.Children.Count >= 3)
                    {
                        HtmlElement fromEle = he.Parent.Children[2];
                        bean.from = fromEle.InnerText;
                    }
                    break;
                }
            }
            return bean;
        }

        public void StartParse()
        {
            result.Clear();
            //<dl class="feed_list" mid="3486490040623407" action-type="feed_list_item">
            HtmlElement ele = web_main.Document.Body;
            HtmlElementCollection dlTag = ele.GetElementsByTagName("dl");
            foreach (HtmlElement he in dlTag)
            {
                //class="feed_list"
                string stylecls = he.GetAttribute("className");
                //mid="3486490040623407"
                string mid = he.GetAttribute("mid");
                if (stylecls == "feed_list" && mid.Length > 0)
                {
                    Weibo2Sfdc.WBContentBean bean = Weibo2Sfdc.WeiboParse.parse(he);

                    

                    result.Add(bean);
                }
            }
        }
    }
}
